package com.newsmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newsmanagement.domain.pojo.RetaBatch;
import com.newsmanagement.domain.pojo.WholBatch;
import com.newsmanagement.mapper.RetaBatchMapper;
import com.newsmanagement.service.RetaBatchService;
import com.newsmanagement.service.WholBatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author adim
 * @description 针对表【reta_batch(零售商产品批号信息表)】的数据库操作Service实现
 * @createDate 2025-10-11 15:17:30
 */
@Service
public class RetaBatchServiceImpl extends ServiceImpl<RetaBatchMapper, RetaBatch> implements RetaBatchService {

    @Autowired
    private WholBatchService wholBatchService;  // 新增依赖注入

    @Override
    public List<RetaBatch> getPendingConfirmByWholId(Integer wholId) {
        // 1. 查询当前批发商的所有批号ID
        List<WholBatch> wholBatches = wholBatchService.getByWholId(wholId);
        if (wholBatches.isEmpty()) {
            return Collections.emptyList();
        }
        List<Integer> wbIds = wholBatches.stream()
                .map(WholBatch::getWbId)
                .collect(Collectors.toList());

        // 2. 查询关联这些批号的待确认零售商批号
        LambdaQueryWrapper<RetaBatch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(RetaBatch::getWbId, wbIds)
                .eq(RetaBatch::getState, 2);  // 2-待确认状态
        return baseMapper.selectList(queryWrapper);
    }
}

